<template>
  <div id="cluereport">
    <el-dialog
      :title="dialogtitle"
      :visible.sync="dialogVisible"
      width="98%" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <personEdit @dialogHide="dialogHide($event)" :personId="personId" :readOnly="readonly" :timestamp="timestamp" :showDept="false" :Formal="false"></personEdit>
    </el-dialog>
    <el-breadcrumb separator=">">
      <!-- <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item> -->
      <!-- <el-breadcrumb-item>人才建设</el-breadcrumb-item> -->
      <el-breadcrumb-item>优秀年轻干部</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxs">
    <!--工具条-->
      <h1>{{unitname + '（' + theYear + '年）'}}</h1>
      <el-form :inline="true" :model="filters" v-if="!sended">
        <div style="padding:22px 22px 0;">
          <el-form-item label="姓名" label-width="50px">
            <el-input v-model="filters.xm" style="width:200px;" placeholder="关键字"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" circle @click="search" icon="el-icon-search"></el-button>
          </el-form-item>
          <el-form-item style="float:right;">
            <el-button type="primary" @click="addnew(false,{})" icon="el-icon-circle-plus-outline" >新增</el-button>
            <el-popconfirm title="确定批量删除所选信息吗？" @confirm="del()">
              <el-button slot="reference" type="danger" icon="el-icon-delete">批量删除</el-button>
            </el-popconfirm>
            <el-popconfirm title="确认立即申报？" style="margin:10px 0 10px 5px" @confirm="sendout">
              <el-button slot="reference" type="success" icon="el-icon-upload2" >申报</el-button>
            </el-popconfirm>
          </el-form-item>
        </div>
      </el-form>
      <div v-else :style="'color:red;font-weight:bolder;height:'+(tableHeight+85)+'px;padding:25px;font-size:var(--size20) !important'" >已报出</div>
    </section>
    <section class="wboxd" style="margin-top:10px" v-if="!sended">
      <!--列表-->
      <el-table :data="list" highlight-current-row v-loading="listLoading" style="width: 100%;" @sort-change='sortChange' :height="tableHeight" border
        @selection-change="handleSelectionChange"
        ref="multipleTable" :row-class-name="tableRowClassName"
        >
        <el-table-column type="index" label="序号" width="50" fixed align="center" class-name="tableth" :index="indexMethod">
        </el-table-column>
        <el-table-column type="selection" width="45" fixed>
        </el-table-column>
        <el-table-column prop="xm" label="姓名" align="center" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="sfzh" label="身份证号" width="200" align="center" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="isOverAge" label="超出年龄范围" width="140" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span :class="{'red-bold': scope.row.isOverAge === 1}">
              {{ scope.row.isOverAge === 1 ? '是' : scope.row.isOverAge === 2 ?'否' : '' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="xb" label="性别" width="100" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            {{scope.row.xbText}}
          </template>
        </el-table-column>
        <el-table-column prop="csrq" label="出生年月" width="150" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
          {{formatDate(scope.row.csrq)}}
          </template>
        </el-table-column>
        <el-table-column prop="mz" label="民族" width="150" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            {{scope.row.mzText}}
          </template>
        </el-table-column>
        <el-table-column prop="zzmm" label="政治面貌" width="150" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            {{scope.row.zzmmText}}
          </template>
        </el-table-column>
        <el-table-column prop="cjgzsj" label="参加工作时间" width="150" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
          {{formatDate(scope.row.cjgzsj)}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250" align="center">
          <template slot-scope="scope">
            <el-button @click="edit(scope.row)" type="primary" size="mini" style="margin:10px 0 10px 5px">编辑</el-button>
            <el-popconfirm title="确定删除该信息吗？" style="margin:10px 0 10px 5px" @confirm="del(scope.row.id)">
              <el-button slot="reference" type="danger" size="mini">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination style="float:right" :total="totalnum" :page-size="pageSize" @current-change="handleCurrentChange" :current-page="pageNum" @size-change="handleSizeChange" :page-sizes="[10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
      </div>
    </section>
  </div>
</template>

<script>
import personEdit from './edit'
export default {
  components: {
    personEdit
  },
  data () {
    return {
      personId: 0,
      ids: [],
      sended: false,
      unitname: '',
      timestamp: '',
      filters: {
        xm: ''
      },
      listLoading: false,
      tableHeight: 100,
      list: [
      ],
      pageNum: 1,
      pageSize: 20,
      totalnum: 10,
      item: {},
      dialogtitle: '新增干部处理',
      dialogVisible: false,
      readonly: false,
      total: 0,
      Isace: 'asc',
      Sortkey: '',
      changeSizeFlag: false,
      userRoles: '',
      theYear: ''
    }
  },
  methods: {
    tableRowClassName ({row}) {
      if (row.isOverAge === 1) {
        return 'youngTableRowAge'
      } else {
        return ''
      }
    },
    indexMethod (index) {
      return index + (this.pageNum - 1) * this.pageSize + 1
    },
    sendout () {
      this.$http
        .post(this.$http.defaults.youngAPI + `api/busUnit/submitToTrial`, {})
        .then(response => {
          if (response.data.success) {
            this.$message({
              message: '申报成功',
              type: 'success'
            })
            this.sended = true
          } else {
            this.$message({
              message: '申报失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '网络申报失败:' + error,
            type: 'error'
          })
        })
    },
    formatDate (str) {
      return str.slice(0, 4) + '.' + str.slice(4, 6)
    },
    inRoles (str) {
      let arr = this.userRoles
      if (arr.indexOf(str) === -1) {
        return false
      } else {
        return true
      }
    },
    checkyoungCadresRole () {
      if (this.inRoles('ROLE_admin') || this.inRoles('ROLE_youngadmin') || this.inRoles('ROLE_sizhang') || this.inRoles('ROLE_fusizhang')) {
        this.$router.push({name: 'youngCadresPersonList'})
      }
      if (this.inRoles('ROLE_chuzhang')) {
        this.$router.push({name: 'youngCadresDeptList'})
      }
      if (this.inRoles('ROLE_banshirenyuan')) {
        this.$router.push({name: 'youngCadresDeptList'})
      }
    },
    handleSelectionChange (val) {
      this.ids = val
    },
    del (id) {
      let ids = []
      if (id) {
        ids = [id]
      } else {
        ids = this.ids.map(item => item.id)
      }
      if (ids.length > 0) {
        this.$http.post(this.$http.defaults.youngAPI + `api/busCandidatePerson/removePersons`, {ids: ids}).then(response => {
          if (response.data.success === true) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            console.log('删除失败:' + response.data.message)
            this.$message({
              message: '删除失败:' + response.data.message,
              type: 'error'
            })
          }
          this.getList()
        })
      } else {
        this.$message({
          message: '请至少选择一条信息',
          type: 'danger'
        })
      }
    },
    dialogHide (flag) {
      this.dialogVisible = false
      if (flag) {
        this.getList()
      }
    },
    addnew (flag, data) {
      this.readonly = false
      this.dialogtitle = '新增'
      this.personId = 0
      data.edittype = 'add'
      this.dialogVisible = true
      this.timestamp = new Date().getTime()
    },
    edit (data) {
      this.readonly = false
      this.dialogtitle = '编辑'
      data.edittype = 'edit'
      this.item = data
      this.personId = data.id
      this.dialogVisible = true
      this.timestamp = new Date().getTime()
    },
    handleSizeChange (val) {
      this.changeSizeFlag = true
      this.pageNum = 1
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      if (this.changeSizeFlag === false) {
        this.pageNum = val
        this.getList()
      }
    },
    // 查询
    search () {
      this.pageNum = 1
      this.getList()
    },
    // 排序
    sortChange: function (column, prop, order) {
      this.Sortkey = column.prop
      if (column.order === 'ascending') {
        this.Isace = 'asc'
      } else {
        this.Isace = 'desc'
      }
      this.getList()
    },
    getList () {
      let para = {
        xm: this.filters.xm,
        dwmc: this.unitname,
        state: 0,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        orderField: 'id',
        orderType: 'desc'
      }
      this.listLoading = true
      this.$http
        .post(this.$http.defaults.youngAPI + `api/busCandidatePerson/queryPersonPage`, para)
        .then(response => {
          if (response.data.success) {
            this.listLoading = false
            this.list = response.data.result.records
            this.totalnum = response.data.result.total
            this.changeSizeFlag = false
            if (response.data.result.current > response.data.result.pages && response.data.result.pages !== 0) {
              this.pageNum = response.data.result.pages
              this.getList()
            }
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      let myheight = 385
      if (this.moreshow) {
        myheight = 385
      } else {
        myheight = 315
      }
      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - myheight
      })
    }
  },
  mounted () {
    this.theYear = this.$route.params.theYear
    this.unitname = localStorage.getItem('youngUnitName')
    this.userRoles = localStorage.getItem('Roles')
    this.getList()
    this.checkyoungCadresRole()
    this.tableresize(this)
    window.onresize = () => {
      this.tableresize(this)
    }
  }
}
</script>
<style>
.youngTableRowAge{
  background: #FFE8E8 !important;
}
#cluereport .el-drawer__wrapper{
  width: 100%;
  height: 100%;
  position: fixed !important;
  overflow: hidden;
}
  .wboxd .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  }
  .wboxd .el-table td, .el-table th{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体'
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #testlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  #cluereport .el-table td{
    border-right: 1px solid #95B8E7;
    border-bottom: 1px solid #95B8E7;
  }
</style>
<style scoped>
  #cluereport .el-button{
    color:#fff !important;
  }
  #cluereport h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  #cluereport .wboxs{
    width: 100%;
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
    padding:0;
  }
  .wboxd{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
  .red-bold {
    color: red;
    font-weight: bold;
  }
</style>
